#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::weak_ptr;
using std::shared_ptr;

weak_ptr<int> wp2;

void test0(){
    shared_ptr<int> sp(new int(10));
    weak_ptr<int> wp(sp);
    cout << *sp << endl;
    /* *wp;//weak_ptr不能直接访问所指向的资源 */

    cout << wp2.use_count() << endl; 


    wp2 = sp;
    cout << wp.use_count() << endl;
    cout << wp2.use_count() << endl;

    shared_ptr<int> sp2 = wp2.lock();
    if(sp2){
        cout << "提升成功,管理的空间还在" << endl;
        cout << *sp2 << endl;
    }else{
        cout << "没有管理有效的空间" << endl;
    }

    cout << endl;
    /* *wp2; */
    cout << wp2.use_count() << endl; 
    bool flag = wp2.expired();
    if(flag){
        cout << "托管的空间已经被销毁" << endl;
    }else{
        cout << "托管的空间还在" << endl;
    }

}

int main(void){
    test0();
    cout << endl;
    shared_ptr<int> sp3 = wp2.lock();
    if(sp3){
        cout << "提升成功,管理的空间还在" << endl;
        cout << *sp3 << endl;
    }else{
        cout << "没有管理有效的空间" << endl;
    }

    bool flag = wp2.expired();
    if(flag){
        cout << "托管的空间已经被销毁" << endl;
    }else{
        cout << "托管的空间还在" << endl;
    }

    return 0;
}
